<?php
require_once '../../modelo/mUser.php';
require "../../cookieAdmin.php";


//require "../Cookie.php";
 
function handler(){
    $objUser = new mUser();
    $mensaje = "";
 
    switch($_SERVER['REQUEST_METHOD']):
        default:
            case 'GET':
                if(!isset($_GET['accion'])):
                    $listado = $objUser->getAll();
 
                    if(isset($_GET['error']))
                        $mensaje = "Operación no realizada";
                    elseif(isset($_GET['satisfactorio']))
                        $mensaje = "Operación realizada";
 
                    include '../../vista/admin/listUser.php';
 


                else:
                    switch($_GET['accion']):
                        case 'agregar':
                            include '../../vista/admin/addUser.php';
                            break;
                        case 'editar':
                            $id = $_GET['id'];
 
                            if($objUser->get($id)):
                                $form = file_get_contents('../../vista/admin/editUser.php');
                                $form = str_replace('{id}', $id, $form);
								$form = str_replace('{nombre}', $objUser->nombre, $form);
                                $form = str_replace('{apellido1}', $objUser->apellido1, $form);
								$form = str_replace('{apellido2}', $objUser->apellido2, $form);
								$form = str_replace('{rol}', $objUser->rol, $form);
								$form = str_replace('{email}', $objUser->email, $form);

							
                                echo $form;

                            else:
                                header('Location: cUser.php?error');
                            endif;
 
                            break;
                        case 'eliminar':
                            $id = $_GET['id'];
 
                            if($objUser->delete($id))
                                header('Location: cUser.php?satisfactorio');
                            else
                                header('Location: cUser.php?error');
 
                            break;
                        default:
                            header('Location: cUser.php');
                            break;
							
                    endswitch;
                endif;
 
            break;
        case 'POST':
            switch($_POST['accion']):
                case 'editar':
					$objUser->nombres = $_POST['nombre'];
					$objUser->apellido1s = $_POST['apellido1'];
					$objUser->apellido2s = $_POST['apellido2'];
					$objUser->rols = $_POST['rol'];
					$objUser->emails = $_POST['email'];

                    if($objUser->update($_POST['id']))
                        header('Location: cUser.php?satisfactorio');
                    else
                        header('Location: cUser.php?error');
 
                    break;
                case 'agregar':
					$objUser->nombres = $_POST['nombre'];
					$objUser->apellido1s = $_POST['apellido1'];
					$objUser->apellido2s = $_POST['apellido2'];
					$objUser->rols= $_POST['rol'];
					$objUser->emails = $_POST['email'];
 
                    if($objUser->set())
                        header('Location: cUser.php?satisfactorio');
                    else
                        header('Location: cUser.php?error');
 
                    break;
                default:
                    header('Location: cUser.php?error');
                    break;
            endswitch;
        break;
    endswitch;
}
handler();
?>